<!DOCTYPE html>
<html>
<head>
<div th:include="base-libs :: base-libs" />
<title>团队列表</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">

<!--[if IE 8.0]>
<style>
	#SHZT{
		position:relative;
		top:3px;
		line-height:30px;
	}
</style>
<![endif]-->
<style>
	.spanBtn{
		font-weight: bold;
		color: blue;
		cursor:pointer;
	}
	.hiddenTd{
		display: none;
	}
</style>
<script>
	var tdManagerData;
	$(function(){
    	initView();
    	initData()
    	$("#btnQuery").trigger("click");
    })
    function initData(){
		tdManagerData = {
				"USERID": parent.userInfo.USERID,
				"YLJGDM": parent.userInfo.YLJGDM,
				"YLJGMC": parent.userInfo.YLJGMC,
// 				"XM": parent.userInfo.XM,
// 				"LXDH":"18679116184",
// 				"LXDZ":"联系地址-测试数据",
// 				"TDJS":1,
				"SHZT":0,
				
// 				"TDMC":"测试团队",
// 				"TDJJ":"团队简介-测试数据"
		}
	}
    	
    function initView() {
    	//删除用户没有的权限按钮
    	$(".powerBtnsBox button").each(function(){
    		var btnId = $(this).attr("id");
    		if(powerBtns.indexOf(btnId) == -1){
    			$(this).remove();
    		}
    	})
    	if($(".powerBtnsBox button").length <= 0){
    		$("#myLayout").layout("remove",'north');
    	}
    	if(powerBtns.indexOf('queryPtAllTdxx') >= 0) $("#SHZT option:eq(1)").remove();
    	
    	enterToQuery(btnQueryHandler);

        $("#combogridYljg").combogrid({
            height: 30
            ,width: 300
            ,panelWidth: 500
            ,singleSelect: true
            ,pagination: true
            ,idField: 'PTJGID'
            ,textField: 'YLJGMC'
            ,mode: "remote"
            ,url: contextRoot + "/yljgxx/queryPage.service"
            ,delay: 1000
            ,reversed: false
            ,columns:[[
                {
                    field:'YLJGMC'
                    ,title:'机构名称'
                    ,width:240
                }
                ,{
                    field:'XZQHMC'
                    ,title:'行政区划'
                    ,width:80
                }
                ,{
                    field:'WSJGLBMC'
                    ,title:'机构类别'
                    ,width:80
                }
                ,{
                    field:'YYDJMC'
                    ,title:'机构等级'
                    ,width:80
                }
            ]]
            ,onBeforeLoad: function(queryParams) {
                var url = $(this).datagrid("options")["url"];
				/*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    queryParams["XZQHDM"] = parent.userInfo.JGXZQHDM;
                    queryParams["YLJGMC"] = queryParams["q"];
                    return true;
                }else{
                    return false;
                }
            },
            onClickRow : function(rowIndex, rowData){
                lastSelectJgmc = rowData.YLJGMC;
                lastSelectJgid = rowData.PTJGID;
            },
        });

        /** 团队列表  */
		$("#tdglTable").datagrid({
				idField : "TDID",
				uniqueId: "TDID",
				rownumbers: true,
				striped : true,
				clickToSelect: true,
				fitColumns: true,
				singleSelect: true,
				fit:true,
				toolbar : "#tdglFormToolbar",
				scrollbarSize:17, //预留的滚动条的宽度
				columns : [[
						{field : "YLJGMC",title : "机构名称",hidden: true,width:120,align:'left',formatter:function(value,row,index){
                            return '<span title="'+ value +'">'+value+'</span>';
                        }},
						{field : "TDMC",title : "团队名称",width:100,align:'left',formatter:function(value,row,index){
                            return '<span title="'+ value +'">'+value+'</span>';
                         }},
						{field : "DZXM",title : "队长",width:60,align:'center'},
						{field : "XBMC",title : "性别",width:60,align:'center'},
						{field : "LXDH",title : "手机号码",width:80,align:'center'},
						{field : "CJSJ",title : "创建日期",width:70,align:'center',formatter:function(value,row,index){
							return value.substring(0,10);
						}},
						{field : "TDCY",title : "团队成员",width:320,formatter:function(value,row,index){
							return '<span title="'+ value +'">'+value+'</span>';
						}},
						{field : "SHZT",title : "当前状态",width:60,align:'center',formatter:function(value,row,index){
							var shztObj = {'0':"暂存",'1':"待审核",'2':"正常",'3':"审核不通过",'4':"已解散",};
							row.SHZT_CN = shztObj[value];
							return shztObj[value];
						}},
						{field : "cz",title : "操作",width:80,align:'center', formatter:function(value,row,index){
							return controlButton(row);
						}} 
				]],
				pagination : true,
				pageNumber : 1,
				pageSize : 10,
				pageList: [10, 20, 30, 40, 50],
				method : "post",
				onBeforeLoad : function(params) {
					if(powerBtns.indexOf('queryJgAllTdxx') >= 0){ //医院管理员
						params.isYygly = 1;
						params.YLJGID = parent.userInfo.PTJGID;
						params.USERID = parent.userInfo.USERID;
						addDownCondition(params,['isYygly','YLJGID','USERID']);
					}else if(powerBtns.indexOf('queryPtAllTdxx') >= 0){ //平台管理员
						$(this).datagrid("showColumn","YLJGMC");
						$(".hiddenTd").show();
						params.isPtgly = 1;
						addDownCondition(params,['isPtgly']);
					}else{
						params.isTdzOrCy = 1;
						params.USERID = parent.userInfo.USERID;
						addDownCondition(params,['isTdzOrCy','USERID']);
					}
					//卫生管理机构
					if(parent.userInfo.WSJGLBDM.indexOf("R") != -1){
                        $(this).datagrid("showColumn","YLJGMC");
                        $(".hiddenTd").show();
                        formatQueryValue(params);
					}
					getContainerDataByClassName('conditionBox','condition',params);//查询条件
				},
		});


	}

	/** 医疗机构查询条件处理*/
    var lastSelectJgmc,lastSelectJgid;
    function formatQueryValue(params){
        var YLJGID = $("#combogridYljg").combobox("getValue");
        var JGMC = $("#combogridYljg").combobox('getText');
        //(1)未选择机构； || (2)机构名称已经不同，但是机构代码还相同
        if(!$("#combogridYljg").combogrid("grid").datagrid("getSelected") || (JGMC != lastSelectJgmc && YLJGID == lastSelectJgid)){
            params.YLJGMC = JGMC;
        }else{
        	params["YLJGID"] = YLJGID;
		}
        lastSelectJgmc = JGMC;
        lastSelectJgid = YLJGID;
    }
    
    //导出条件与查询条件同步
    function addDownCondition(params,fieldArr){
    	var downCondition = $("#downCondition");
    	$.each(fieldArr, function(i,item){
    		var input = $("<input>").attr({"name":item,'hidden':'hidden'}).val(params[item]).css('display','none');
    		downCondition.append(input);
    	})
    }
    
    /** 操作按钮控制  */
    function controlButton(row){
    	row.CJSJ = row.CJSJ.substring(0,10);
		var SHZT = row.SHZT;

		var isTDZ = (row.USERID==parent.userInfo.USERID);//是否为团队长在登陆
		var isBoss = !StringUtils.isEmpty($("#deleteTdxx").html());  //是否是医院管理员在登陆
		
		row = JSON.stringify(row).replace(/"/g, '&quot;');
		var btns;
		if(isBoss && SHZT == 1){
            btns = '<span class="spanBtn" onclick="checkTdxx(' + row + ')">审核</span>';
		}else{
            btns = '<span class="spanBtn" onclick="queryDetail(' + row + ')">详细</span>';
		}
		if(isTDZ && (SHZT == 0 || SHZT == 3 )){
            btns += '&nbsp;&nbsp;<span class="spanBtn" onclick="updateTdxx(' + row + ')">维护团队</span>';
		}else if(isBoss && (SHZT == 0 || SHZT == 2)){
            btns += '&nbsp;&nbsp;<span class="spanBtn" onclick="updateTdxx(' + row + ')">维护团队</span>';
		}
		
		return btns;
    }
    	
    	
   	/** 跳转到添加页面  */
   	function toManager(type,updateData){
   		StoreCache.setCache('forbackUrl','index.html');
   		if(type=='update'){
   			//tdManagerData = $("#tdglTable").datagrid("getSelected");
   			tdManagerData = updateData;
   		}
   		tdManagerData.type=type; //type='add' || type='update'
   		StoreCache.setCache("tdManagerData",tdManagerData);
   		$.QuickUrlUtils.redirectUrl("manager.html");
   	}
   	
   	/** 查询  */
   	function btnQueryHandler(){
   		$("#tdglTable").data("query",true).datagrid({"url": contextRoot + "tdgl/queryPage.service"});
   	}
   	
   	/** 导出  */
   	function downTdxx(){
   		$("#downloadForm")[0].submit();
   	}
   	
   	/** 维护团队  */
   	function updateTdxx(updateData){
   		var boss = $("#deleteTdxx").html();
   		if(!StringUtils.isEmpty(boss) && updateData.SHZT == 2){
	   		StoreCache.setCache("isBoss",true); //医院管理员正在维护一条正常的记录
   		}
   		toManager("update",updateData);
   	}
   	
   	/** 解散团队  */
   	function  deleteTdxx(){
   		var tdArr = $("#tdglTable").datagrid("getSelections");
   		if(tdArr.length==0){
   			$.QuickAlert.alertFail({content : "请选择一个团队"});
   		}else if(tdArr.length>1){
   			$.QuickAlert.alertFail({content : "请选中单条记录！"});
   		}else{
   			var tdInfo = tdArr[0];
			if(tdInfo.SHZT == 2){
		   		$.QuickAlert.alertConfirm({
					content : "解散团队后不可恢复，是否继续？",
					callback : function() {
						var params = {
							TDID: tdInfo.TDID,
							SHZT:4, //已解散
							TDZT:0, //不可用
							JGID: parent.userInfo.PTJGID,
							CZRID: parent.userInfo.USERID
						}
				   		$.QuickRemote.AjaxJson(contextRoot + "tdgl/delete", deleteTdxxBack, params);
					}
				})
	   		}else{
	   			$.QuickAlert.alertFail({content : "请选择一条状态为正常的团队"});
	   		}
   		}
   	}
   	/** 解散团队回调  */
   	function deleteTdxxBack(result){
   		if(result.state){
	   		$.QuickAlert.alertNormal(result.info.success, {callback: function(){
	   			$("#tdglTable").removeData("tdInfo");
				$("#btnQuery").trigger("click");
			}});
   		}else{
   			$.QuickAlert.alertFail({content: result.info["faultInfo"]});
   		}
   	}
   	
   	/** 查看详细  */
   	function queryDetail(rowData){
   		rowData.YLJGMC = parent.userInfo.YLJGMC;
   		var boss = $("#deleteTdxx").html();
   		if(!StringUtils.isEmpty(boss)){
	   		StoreCache.setCache("isBoss",true);
   		}
   		//平台管理员
   		if(powerBtns.indexOf('queryPtAllTdxx') >= 0){
   			StoreCache.setCache("isPtgly",true);
   		}
   		StoreCache.setCache("tdManagerData",rowData);
   		$.QuickUrlUtils.redirectUrl("detail.html");
   	}
   	
   	function checkTdxx(tdInfo){
  		tdInfo.YLJGMC = parent.userInfo.YLJGMC;
  		tdInfo.XM = tdInfo.TDXM;
  	   	StoreCache.setCache("tdManagerData",tdInfo);
  		$.QuickUrlUtils.redirectUrl("check.html");
   	}
</script>

</head>

<body>
	<div class="easyui-layout" fit="true" border="false" id="layout">
		<div region="north" style="overflow:hidden;padding:15px 0px;background:#f4f4f4;" border="false" id="north">
			<form action="downTdxxToExcel.service" method="post" enctype="multipart/form-data" id="downloadForm">
				<div id="downCondition"></div>
				
				<table class="form-table" id="conditionBox">
					<tr>
						<td class="td-text hiddenTd">机构名称：</td>
						<td class="td-content hiddenTd">
							<input id="combogridYljg" class="form-control"  style="width: 100%;">
						</td>
						<td class="td-text">团队名称：</td>
						<td class="td-content">
							<input id="TDMC" name="TDMC" class="form-control condition"  style="width: 100%;">
						</td>
						<td class="td-text">状态：</td>
						<td class="td-content">
							<select id="SHZT" name="SHZT" class="form-control condition" style="width: 100%;">
								<option value="">请选择</option>
								<option value="0">暂存</option>
								<option value="1">待审核</option>
								<option value="2">正常</option>
								<option value="3">审核不通过</option>
								<option value="4">已解散</option>
							</select>
						</td>
						<td class="td-text" style="width:170px;text-align:center">
							<button id="btnQuery" class="btn btn-info" type="button" onclick="btnQueryHandler()">查询</button>
						</td>
					</tr>
				</table>
			</form>	
		</div>
		<div region="center" border="false">
			<div class="easyui-layout" border="false" fit="true" id="myLayout">
				<div region="north" border="false" style="height:40px;line-height:40px;padding-left:20px;background:#f4f4f4" class="powerBtnsBox">
						<button type="button" onclick="downTdxx()" id="download">导出</button>
						<!-- <button type="button" onclick="toManager('add')" id="addTdxx">新建团队</button> -->
						<!-- <button type="button" onclick="updateTdxx()" id="updateTdxx">维护团队</button> -->
						<!-- <button type="button" onclick="checkTdxx()" id="checkTdxx">审核团队</button> -->
						<button type="button" onclick="deleteTdxx()" id="deleteTdxx">解散团队</button>
				</div>
				<div region="center" border="false">
					<table id="tdglTable" border="false"></table>
				</div>
			</div>
		</div>
	</div>
	
</body>
</html>
